/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.dialog;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;

import de.renier.jkeepass.Messages;
import de.renier.jkeepass.util.Utils;


/**
 * CreatePasswordDialog
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class CreatePasswordDialog extends JDialog {

  private static final long serialVersionUID = 1L;
  public static final int RESULT_CANCEL = 0; 
  public static final int RESULT_OK = 1; 
  private JPanel jContentPane = null;
  private JPanel buttonPanel = null;
  private JPanel mainPanel = null;
  private JButton submitButton = null;
  private JButton cancelButton = null;
  private JLabel jLabel = null;
  private JLabel jLabel1 = null;
  private JButton passwordButton = null;
  private JPasswordField passwordPasswordField = null;
  private JPasswordField passwordSubmitPasswordField = null;
  private int result = RESULT_CANCEL;

  /**
   * @param owner
   */
  public CreatePasswordDialog(Frame owner) {
    super(owner);
    initialize();
  }

  /**
   * This method initializes this
   * 
   * @return void
   */
  private void initialize() {
    this.setSize(441, 200);
    this.setTitle(Messages.getString("CreatePasswordDialog.0")); //$NON-NLS-1$
    this.setModal(true);
    this.setContentPane(getJContentPane());
    this.setLocationRelativeTo(this.getOwner());
    getRootPane().setDefaultButton(getSubmitButton());
  }

  /**
   * This method initializes jContentPane
   * 
   * @return javax.swing.JPanel
   */
  private JPanel getJContentPane() {
    if (jContentPane == null) {
      jContentPane = new JPanel();
      jContentPane.setLayout(new BorderLayout());
      jContentPane.add(getButtonPanel(), BorderLayout.SOUTH);
      jContentPane.add(getMainPanel(), BorderLayout.CENTER);
    }
    return jContentPane;
  }

  /**
   * This method initializes buttonPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getButtonPanel() {
    if (buttonPanel == null) {
      buttonPanel = new JPanel();
      buttonPanel.setLayout(new FlowLayout());
      buttonPanel.add(getSubmitButton(), null);
      buttonPanel.add(getCancelButton(), null);
    }
    return buttonPanel;
  }

  /**
   * This method initializes mainPanel	
   * 	
   * @return javax.swing.JPanel	
   */
  private JPanel getMainPanel() {
    if (mainPanel == null) {
      GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
      gridBagConstraints4.fill = GridBagConstraints.HORIZONTAL;
      gridBagConstraints4.gridy = 1;
      gridBagConstraints4.weightx = 1.0;
      gridBagConstraints4.insets = new Insets(0, 0, 5, 10);
      gridBagConstraints4.gridx = 1;
      GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
      gridBagConstraints3.fill = GridBagConstraints.HORIZONTAL;
      gridBagConstraints3.gridy = 0;
      gridBagConstraints3.weightx = 1.0;
      gridBagConstraints3.insets = new Insets(0, 0, 5, 10);
      gridBagConstraints3.gridx = 1;
      GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
      gridBagConstraints2.gridx = 0;
      gridBagConstraints2.gridwidth = 2;
      gridBagConstraints2.gridy = 2;
      GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
      gridBagConstraints1.gridx = 0;
      gridBagConstraints1.anchor = GridBagConstraints.WEST;
      gridBagConstraints1.insets = new Insets(0, 10, 5, 5);
      gridBagConstraints1.gridy = 1;
      jLabel1 = new JLabel();
      jLabel1.setText(Messages.getString("CreatePasswordDialog.1")); //$NON-NLS-1$
      GridBagConstraints gridBagConstraints = new GridBagConstraints();
      gridBagConstraints.gridx = 0;
      gridBagConstraints.anchor = GridBagConstraints.WEST;
      gridBagConstraints.insets = new Insets(0, 10, 5, 5);
      gridBagConstraints.gridy = 0;
      jLabel = new JLabel();
      jLabel.setText(Messages.getString("CreatePasswordDialog.2")); //$NON-NLS-1$
      mainPanel = new JPanel();
      mainPanel.setLayout(new GridBagLayout());
      mainPanel.add(jLabel, gridBagConstraints);
      mainPanel.add(jLabel1, gridBagConstraints1);
      mainPanel.add(getPasswordButton(), gridBagConstraints2);
      mainPanel.add(getPasswordPasswordField(), gridBagConstraints3);
      mainPanel.add(getPasswordSubmitPasswordField(), gridBagConstraints4);
    }
    return mainPanel;
  }

  /**
   * This method initializes submitButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getSubmitButton() {
    if (submitButton == null) {
      submitButton = new JButton();
      submitButton.setText(Messages.getString("CreatePasswordDialog.3")); //$NON-NLS-1$
      submitButton.setIcon(new ImageIcon(getClass().getResource("/org/javalobby/icons/20x20/Key.gif"))); //$NON-NLS-1$
      submitButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          // check if password are both set korrekt
          String passwordText = String.valueOf(passwordPasswordField.getPassword());
          if (!Utils.isEmpty(passwordText)) {
            String passwordSubmitText = String.valueOf(passwordSubmitPasswordField.getPassword());
            if (passwordText.equals(passwordSubmitText)) {
              result = RESULT_OK;
              dispose();
              return;
            }
          }
          // something is wrong
          JOptionPane.showConfirmDialog(CreatePasswordDialog.this,Messages.getString("CreatePasswordDialog.5"), Messages.getString("CreatePasswordDialog.6") , JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$
        }
      });
    }
    return submitButton;
  }

  /**
   * This method initializes cancelButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getCancelButton() {
    if (cancelButton == null) {
      cancelButton = new JButton();
      cancelButton.setIcon(new ImageIcon(getClass().getResource("/org/javalobby/icons/20x20/Delete.gif"))); //$NON-NLS-1$
      cancelButton.setText(Messages.getString("CreatePasswordDialog.8")); //$NON-NLS-1$
      cancelButton.addActionListener(new java.awt.event.ActionListener() {
        public void actionPerformed(java.awt.event.ActionEvent e) {
          result = RESULT_CANCEL;
          dispose();
        }
      });
    }
    return cancelButton;
  }

  /**
   * This method initializes passwordButton	
   * 	
   * @return javax.swing.JButton	
   */
  private JButton getPasswordButton() {
    if (passwordButton == null) {
      passwordButton = new JButton();
      passwordButton.setText(Messages.getString("CreatePasswordDialog.9")); //$NON-NLS-1$
      passwordButton.addActionListener(new java.awt.event.ActionListener() {
        private boolean passwordVisible = false;
        public void actionPerformed(java.awt.event.ActionEvent e) {
          if (passwordVisible) {
            getPasswordPasswordField().setEchoChar('*');
            getPasswordSubmitPasswordField().setEchoChar('*');
            passwordButton.setText(Messages.getString("CreatePasswordDialog.10")); //$NON-NLS-1$
          } else {
            getPasswordPasswordField().setEchoChar((char)0);
            getPasswordSubmitPasswordField().setEchoChar((char)0);
            passwordButton.setText(Messages.getString("CreatePasswordDialog.11")); //$NON-NLS-1$
          }
          passwordVisible = !passwordVisible;
        }
      });
    }
    return passwordButton;
  }

  /**
   * This method initializes passwordPasswordField	
   * 	
   * @return javax.swing.JPasswordField	
   */
  private JPasswordField getPasswordPasswordField() {
    if (passwordPasswordField == null) {
      passwordPasswordField = new JPasswordField();
    }
    return passwordPasswordField;
  }

  /**
   * This method initializes passwordSubmitPasswordField	
   * 	
   * @return javax.swing.JPasswordField	
   */
  private JPasswordField getPasswordSubmitPasswordField() {
    if (passwordSubmitPasswordField == null) {
      passwordSubmitPasswordField = new JPasswordField();
    }
    return passwordSubmitPasswordField;
  }

  /**
   * getResult
   *
   * @return Returns the result.
   */
  public int getResult() {
    return result;
  }
  
  /**
   * getPassword
   *
   * @return
   */
  public String getPassword() {
    return String.valueOf(passwordPasswordField.getPassword());
  }

}  //  @jve:decl-index=0:visual-constraint="10,10"
